package leader.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import beanDao.MapToBean;

import teacher.bean.Paper;
import teacher.bean.Teacher;
import teacher.bean.Treatise;
import tools.StringDate;
import database.dao.MysqlCrud;

public class DirectTreatiseService {
	/**
	 * @category 查询同一研究方向的所有期刊论文
	 * @author chenkai
	 * @param teacher Teacher 研究方向信息
	 * @return lists List<Paper> 返回以List<Paper>的论文集合
	 */
	public List<Treatise> selectAllDirectTreatise(Teacher teacher) {
		List<Treatise> lists = new ArrayList<Treatise>();
		String sql="select * from (select * from treatise order by state,class,date desc) as acheve ,teacher as T where acheve.teaId = T.teaId and T.direction=? and acheve.state<>?";
		String[] parameter={
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list= mysqlcurd.SelectData(sql, parameter);
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToTreatise(map));
		}
		return lists;
	}
	
	/**
	 * @category 查询同一研究方向的所有期刊论文
	 * @author chenkai
	 * @param teacher Teacher 研究方向信息
	 * @return lists List<Paper> 返回以List<Paper>的论文集合
	 */
	public List<Treatise> selectSegregateAllDirectTreatise(Teacher teacher,int pageNow,int pageSize) {
		List<Treatise> lists = new ArrayList<Treatise>();
		String sql="select * from (select * from treatise order by state,class,date desc) as acheve ,teacher as T where acheve.teaId = T.teaId and T.direction=? and acheve.state<>? and treId LIMIT " +
				""+(pageNow-1)+" ,"+pageSize+"";
		String[] parameter={
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list= mysqlcurd.SelectData(sql, parameter);
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToTreatise(map));
		}
		return lists;
	}
	/**
	 * @category 通过名称模糊查询一个研究方向的一篇论著
	 * @param vague 查询的论著的名称
	 * @param leader 传入的研究方向
	 * @return treatiseOne List<Treatise> 返回一个List<Treatise>的论著集合
	 */
	public List<Treatise> selectTreatiseLikeName(String vague,Teacher leader) {
		String sql= "select * from (select * from treatise order by state,class,date desc) as acheve,teacher as T where acheve.teaId=T.teaId and acheve.state<>? and acheve.name like '%"+vague+"%' and T.direction=?";
		String[] parameter={
				String.valueOf(1),
				String.valueOf(leader.getDirection())
				};
		List<Treatise> lists=new ArrayList<Treatise>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list = mysqlcurd.SelectData(sql,parameter);
		//System.out.println(list.size());
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToTreatise(map));
		}
		return lists;
	}
	
	/**
	 * @category 通过名称模糊查询一个研究方向的一篇论著
	 * @param vague 查询的论著的名称
	 * @param leader 传入的研究方向
	 * @return treatiseOne List<Treatise> 返回一个List<Treatise>的论著集合
	 */
	public List<Treatise> selectSegregateTreatiseLikeName(String vague,Teacher leader,int pageNow,int pageSize) {
		String sql= "select * from (select * from treatise order by state,class,date desc) as acheve,teacher as T where acheve.teaId=T.teaId and acheve.name like '%"+vague+"%' and T.direction=? and acheve.state<>? and treId LIMIT " +
				""+(pageNow-1)+" ,"+pageSize+"";
		String[] parameter={
				String.valueOf(leader.getDirection()),
				String.valueOf(1)
				};
		List<Treatise> lists=new ArrayList<Treatise>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list = mysqlcurd.SelectData(sql,parameter);
		//System.out.println(list.size());
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToTreatise(map));
		}
		return lists;
	}
	
	/**
	 * @category 通过时间段查询方向论著论著
	 * @param date1 时间段的下限
	 * @param date2 时间段的上限
	 * @param leader 查询的方向
	 * @return treatiseOne List<Treatise> 返回查询到的List<Treatise>论著列表
	 */
	public List<Treatise> selectDirectTreatiseByTime(Date date1,Date date2,Teacher leader) {
		String sql= "select * from (select * from treatise order by state,class,date desc) as acheve,teacher as T where acheve.teaId=T.teaId and date>='"+date1+"' and date<='"+date2+"' and T.direction=? and acheve.state<>?";
		String[] parameter={
				String.valueOf(leader.getDirection()),
				String.valueOf(1)
				};
		List<Treatise> lists=new ArrayList<Treatise>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list = mysqlcurd.SelectData(sql, parameter);
		//System.out.println(list.size());
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToTreatise(map));
		}
		return lists;
	}
	
	/**
	 * @category 通过时间段查询方向论著论著
	 * @param date1 时间段的下限
	 * @param date2 时间段的上限
	 * @param leader 查询的方向
	 * @return treatiseOne List<Treatise> 返回查询到的List<Treatise>论著列表
	 */
	public List<Treatise> selectSegregateDirectTreatiseByTime(Date date1,Date date2,Teacher leader,int pageNow,int pageSize) {
		String sql= "select * from (select * from treatise order by state,class,date desc) as acheve,teacher as T where acheve.teaId=T.teaId and date>='"+date1+"' and date<='"+date2+"' and T.direction=? and acheve.state<>? and treId LIMIT " +
				""+(pageNow-1)+" ,"+pageSize+"";
		String[] parameter={
				String.valueOf(leader.getDirection()),
				String.valueOf(1)
				};
		List<Treatise> lists=new ArrayList<Treatise>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list = mysqlcurd.SelectData(sql, parameter);
		//System.out.println(list.size());
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToTreatise(map));
		}
		return lists;
	}
	
	/**
	 * @category 查询同一研究方向的所有期刊论文
	 * @author chenkai
	 * @param teacher Teacher 研究方向信息
	 * @return lists List<Paper> 返回以List<Paper>的论文集合
	 */
	public List<Treatise> selectDirectTreatiseByState(Teacher teacher,Treatise tre) {
		List<Treatise> lists = new ArrayList<Treatise>();
		String sql="select * from (select * from treatise order by state,class,date desc) as acheve ,teacher as T where acheve.teaId = T.teaId and acheve.state = ? and T.direction= ? and acheve.state<>?";
		String[] parameter={
				String.valueOf(tre.getState()),
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list= mysqlcurd.SelectData(sql, parameter);
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToTreatise(map));
		}
		return lists;
	}
	
	/**
	 * @category 查询同一研究方向的所有期刊论文
	 * @author chenkai
	 * @param teacher Teacher 研究方向信息
	 * @return lists List<Paper> 返回以List<Paper>的论文集合
	 */
	public List<Treatise> selectSegregateDirectTreatiseByState(Teacher teacher,Treatise tre,int pageNow,int pageSize) {
		List<Treatise> lists = new ArrayList<Treatise>();
		String sql="select * from (select * from treatise order by state,class,date desc) as acheve ,teacher as T where acheve.teaId = T.teaId and acheve.state = ? and T.direction=? and acheve.state<>? and treId LIMIT " +
				""+(pageNow-1)+" ,"+pageSize+"";
		String[] parameter={
				String.valueOf(tre.getState()),
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list= mysqlcurd.SelectData(sql, parameter);
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToTreatise(map));
		}
		return lists;
	}
	/**
	 * @category 通过ID查询论著
	 * @author chenkai
	 * @param treatise Treatise 论著ID
	 * @return treatise Treatise 返回一条论著
	 */
	public Treatise selectTreatiseById(Treatise treatise) {
		String sql= "select * from treatise where treId=?";
		String[] parameter={
				String.valueOf(treatise.getTreId()),
				String.valueOf(1)
				};
		Treatise t=new Treatise();
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list = mysqlcurd.SelectData(sql,parameter);
		if(list.size()>0&&list!=null)
		{	
			Map map=list.get(0);
			t=new MapToBean().MapToTreatise(map);
		}
		return t;
	}
	
	public boolean updateTreatiseById(Treatise treatise) {
		String sql="update leader set name = ?, author = ?, date = ? ,state = ? , class = ? , press = ? , abstrate = ? , upload = ? , bookNum = ? where leaId = ?";
		String[] parameter={
				String.valueOf(treatise.getName()),
				String.valueOf(treatise.getAuthor()),
				String.valueOf(treatise.getDate()),
				String.valueOf(treatise.getState()),
				String.valueOf(treatise.getClass_()),
				String.valueOf(treatise.getPress()),
				String.valueOf(treatise.getAbstract_()),
				String.valueOf(treatise.getUpload()),
				String.valueOf(treatise.getBookNum()),
				String.valueOf(treatise.getTreId())
				};
		boolean flag = false;
		MysqlCrud mysqlcurd=new MysqlCrud();
		flag = mysqlcurd.InsertAndUpdateData(sql, parameter);
		return flag;
	}
	
	/**
	 * @category 论著的审核
	 * @author chenkai
	 * @param paper Paper 传入审核的论著以及审核结果
	 * @return flag boolean
	 */
	public boolean updateTreatiseState(Treatise treatise) {
		boolean flag = false;
		String sql="update treatise set state = ? where treId = ?";
		String[] parameter={
				String.valueOf(treatise.getState()),
				String.valueOf(treatise.getTreId())
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		flag = mysqlcurd.InsertAndUpdateData(sql, parameter);
		return flag;
	}
	
	/**
	 * @category 得到一个方向的所有项目的数量
	 * @author chenkai
	 * @return sum int 教师数量
	 */
	public int getCountOfDirectTreatise(Teacher teacher) {
		int sum=0;
		String sql="select count(*) from treatise as acheve ,teacher as T where acheve.teaId = T.teaId and T.direction=? and acheve.state<>?";
		String[] parameter={
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql,parameter);
		Map map=lists.get(0);
		sum=Integer.parseInt(map.get(0).toString());
		return sum;
	}
	
	/**
	 * @category 得到一个方向的所有项目的数量
	 * @author chenkai
	 * @return sum int 教师数量
	 */
	public int getCountOfDirectTreatiseByTime(Teacher teacher,Date date1,Date date2) {
		int sum=0;
		String sql="select count(*) from treatise as acheve ,teacher as T where acheve.teaId = T.teaId and T.direction=? and acheve.state<>? and date>='"+date1+"' and date<='"+date2+"'";
		String[] parameter={
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql,parameter);
		Map map=lists.get(0);
		sum=Integer.parseInt(map.get(0).toString());
		return sum;
	}
	
	/**
	 * @category 得到一个方向的所有项目的数量
	 * @author chenkai
	 * @return sum int 教师数量
	 */
	public int getCountOfDirectTreatiseByName(Teacher teacher,String vague) {
		int sum=0;
		String sql="select count(*) from treatise as acheve ,teacher as T where acheve.teaId = T.teaId and T.direction=? and acheve.state<>? and acheve.name like '%"+vague+"%'";
		String[] parameter={
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql,parameter);
		Map map=lists.get(0);
		sum=Integer.parseInt(map.get(0).toString());
		return sum;
	}
	
	/**
	 * @category 得到一个方向的所有项目的数量
	 * @author chenkai
	 * @return sum int 教师数量
	 */
	public int getCountOfDirectTreatiseByState(Teacher teacher,Treatise tre) {
		int sum=0;
		String sql="select count(*) from treatise as acheve ,teacher as T where acheve.teaId = T.teaId and acheve.state = ? and T.direction=? and acheve.state<>?";
		String[] parameter={
				String.valueOf(tre.getState()),
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql,parameter);
		Map map=lists.get(0);
		sum=Integer.parseInt(map.get(0).toString());
		return sum;
	}
}
